const serviceCommon = {

    login: (name, method, value) => {
        return new Promise((resolve, reject) => {
            if (value) {
                name[method](value).then((results) => {
                    if (results > 0) {
                        let data = {code: 1, msg: '登录成功！'};
                        resolve(data);
                    } else {
                        let data = {code: 2, msg: '登录失败（用户名或密码错误）！'};
                        resolve(data);
                    }
                }, (err) => {
                    let data = {code: 3, msg: err.message};
                    reject(data);
                }).catch((error) => {
                    let data = {code: 3, msg: error.message};
                    reject(data);
                })
            } else {
                name[method]().then((results) => {
                    if (results > 0) {
                        let data = {code: 1, msg: '登录成功！'};
                        resolve(data);
                    } else {
                        let data = {code: 2, msg: '登录失败（用户名或密码错误）！'};
                        resolve(data);
                    }
                }, (err) => {
                    let data = {code: 3, msg: err.message};
                    reject(data);
                }).catch((error) => {
                    let data = {code: 3, msg: error.message};
                    reject(data);
                })
            }
        })
    },
    //获取数据列表
    getListMethod: (name, method, value) => {
        return new Promise((resolve, reject) => {
            if (value) {
                name[method](value).then((results) => {
                    if (results.length > 0) {
                        let data = {code: 1, msg: '获取成功！', data: results};
                        resolve(data);
                    } else {
                        let data = {code: 2, msg: '没有数据！'};
                        resolve(data);
                    }
                }, (err) => {
                    let data = {code: 3, msg: err.message};
                    reject(data);
                }).catch((error) => {
                    let data = {code: 3, msg: error.message};
                    reject(data);
                })
            } else {
                name[method]().then((results) => {
                    if (results.length > 0) {
                        let data = {code: 1, msg: '获取成功！', data: results};
                        resolve(data);
                    } else {
                        let data = {code: 2, msg: '没有数据！'};
                        resolve(data);
                    }
                }, (err) => {
                    let data = {code: 3, msg: err.message};
                    reject(data);
                }).catch((error) => {
                    let data = {code: 3, msg: error.message};
                    reject(data);
                })
            }
        })
    },
    //增加数据
    addDataMethod: (name, method, value) => {
        return new Promise((resolve, reject) => {
            if (value) {
                name[method](value).then((results) => {
                    if (results > 0) {
                        let data = {code: 1, msg: '添加成功！'};
                        resolve(data);
                    } else {
                        let data = {code: 2, msg: '添加失败！'};
                        resolve(data);
                    }
                }, (err) => {
                    let data = {code: 3, msg: err.message};
                    reject(data);
                }).catch((error) => {
                    let data = {code: 3, msg: error.message};
                    reject(data);
                })
            } else {
                name[method]().then((results) => {
                    if (results > 0) {
                        let data = {code: 1, msg: '添加成功！'};
                        resolve(data);
                    } else {
                        let data = {code: 2, msg: '添加失败！'};
                        resolve(data);
                    }
                }, (err) => {
                    let data = {code: 3, msg: err.message};
                    reject(data);
                }).catch((error) => {
                    let data = {code: 3, msg: error.message};
                    reject(data);
                })
            }
        })
    },
    //编辑数据
    editDataMethod: (name, method, value) => {
        return new Promise((resolve, reject) => {
            if (value) {
                name[method](value).then((results) => {
                    if (results > 0) {
                        let data = {code: 1, msg: '修改成功！'};
                        resolve(data);
                    } else {
                        let data = {code: 2, msg: '修改失败！'};
                        resolve(data);
                    }
                }, (err) => {
                    let data = {code: 3, msg: err.message};
                    reject(data);
                }).catch((error) => {
                    let data = {code: 3, msg: error.message};
                    reject(data);
                })
            } else {
                name[method]().then((results) => {
                    if (results > 0) {
                        let data = {code: 1, msg: '修改成功！'};
                        resolve(data);
                    } else {
                        let data = {code: 2, msg: '修改失败！'};
                        resolve(data);
                    }
                }, (err) => {
                    let data = {code: 3, msg: err.message};
                    reject(data);
                }).catch((error) => {
                    let data = {code: 3, msg: error.message};
                    reject(data);
                })
            }
        })
    },
    //删除数据
    deleteDataMethod: (name, method, value) => {
        return new Promise((resolve, reject) => {
            if (value) {
                name[method](value).then((results) => {
                    if (results > 0) {
                        let data = {code: 1, msg: '删除成功！'};
                        resolve(data);
                    } else {
                        let data = {code: 2, msg: '删除失败！'};
                        resolve(data);
                    }
                }, (err) => {
                    let data = {code: 3, msg: err.message};
                    reject(data);
                }).catch((error) => {
                    let data = {code: 3, msg: error.message};
                    reject(data);
                })
            } else {
                name[method]().then((results) => {
                    if (results > 0) {
                        let data = {code: 1, msg: '删除成功！'};
                        resolve(data);
                    } else {
                        let data = {code: 2, msg: '删除失败！'};
                        resolve(data);
                    }
                }, (err) => {
                    let data = {code: 3, msg: err.message};
                    reject(data);
                }).catch((error) => {
                    let data = {code: 3, msg: error.message};
                    reject(data);
                })
            }
        })
    },
    //获取数据总数
    getSizeMethod: (name, method, value) => {
        return new Promise((resolve, reject) => {
            if (value) {
                name[method](value).then((results) => {
                    if (results > 0) {
                        let data = {code: 1, msg: '获取成功！', count: results};
                        resolve(data);
                    } else {
                        let data = {code: 2, msg: '没有数据！'};
                        resolve(data);
                    }
                }, (err) => {
                    let data = {code: 3, msg: err.message};
                    reject(data);
                }).catch((error) => {
                    let data = {code: 3, msg: error.message};
                    reject(data);
                })
            } else {
                name[method]().then((results) => {
                    if (results > 0) {
                        let data = {code: 1, msg: '获取成功！', count: results};
                        resolve(data);
                    } else {
                        let data = {code: 2, msg: '没有数据！'};
                        resolve(data);
                    }
                }, (err) => {
                    let data = {code: 3, msg: err.message};
                    reject(data);
                }).catch((error) => {
                    let data = {code: 3, msg: error.message};
                    reject(data);
                })
            }
        })
    }
};
module.exports = serviceCommon;